package com.competition.oss.controller;

import com.competition.commonutils.R;
import com.competition.oss.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/oss")
@CrossOrigin
@Api(description = "oss上传文件")
public class OssController {

    @Autowired
    private OssService ossService;

    @PostMapping("upload")
    @ApiOperation("上传文件，视频除外")
    public R uploadOssFile(MultipartFile file) throws IOException {  //上传文件
        String url=ossService.uploadFileAvatar(file);
        return R.ok().data("url",url);
    }
    @PostMapping("batchdelete")
    public R batchDeleteFile(String[] fileNames){
        List<String> keys = new ArrayList<String>();
        for(int i=0;i<fileNames.length;i++){
            keys.add(fileNames[i]);
            System.out.println(fileNames[i]);
        }
        ossService.batchDelete(keys);
        System.out.println(fileNames.length);
        return R.ok();
    }

    @PostMapping("deletefile")
    public R deleteFile(String fileNames){
        ossService.deleteFile(fileNames);
        return R.ok();
    }
}
